﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using DTO;
using BUS;

namespace EXAM.Admin.Page_Admin
{
    public partial class FileManager : System.Web.UI.Page
    {
        int _FileDel = 9;
        string sErr = "";
        string _db = "EXA";
        public bool allowDelete = false;
        protected void Page_Load(object sender, EventArgs e)
        {

            //set properties according to configuration panel
            FileExplorer1.VisibleControls = GetVisibleControls();
            FileExplorer1.EnableOpenFile = enableOpenFile.Checked;
            FileExplorer1.DisplayUpFolderItem = displayUpFolder.Checked;
            FileExplorer1.AllowPaging = allowPaging.Checked;
            FileExplorer1.EnableCreateNewFolder = enableCreateNewFolder.Checked;

            if (!enableUpload.Checked)
            {
                FileExplorer1.Configuration.UploadPaths = new string[0];
            }
            if (!IsPostBack)
            {
                //Set initial folder to open. Note that the path is case sensitive!
                FileExplorer1.InitialPath = Page.ResolveUrl("~/Upload/undelete.me");

                
            }
            if (Session["UserAdmin"] != null)
                SetPermission(Session["UserAdmin"] as DTO.UserInfo);
        }
        private void SetPermission(UserInfo userInfo)
        {
            POPControl ctr = new POPControl();
            DTO.POPInfo inf = ctr.Get(userInfo.GroupCode, _db, ref sErr);
            if (inf.ROLE_ID != "")
            {
                string permis = GetPermission(inf, _FileDel);
                if (permis == "N")
                {
                    allowDelete = true;
                }

            }
        }
        private string GetPermission(POPInfo user, int pos)
        {
            string permis = user.PERMISSION.Substring(pos - 1, 1);
            if (permis == " ")
                permis = user.DEFAULT_VALUE;
            return permis;
        }
        protected Telerik.Web.UI.FileExplorer.FileExplorerControls GetVisibleControls()
        {
            Telerik.Web.UI.FileExplorer.FileExplorerControls explorerControls = 0;
            if (visibleControls.Items[0].Selected)
                explorerControls |= Telerik.Web.UI.FileExplorer.FileExplorerControls.AddressBox;
            if (visibleControls.Items[1].Selected)
                explorerControls |= Telerik.Web.UI.FileExplorer.FileExplorerControls.Grid;
            if (visibleControls.Items[2].Selected)
                explorerControls |= Telerik.Web.UI.FileExplorer.FileExplorerControls.Toolbar;
            if (visibleControls.Items[3].Selected)
                explorerControls |= Telerik.Web.UI.FileExplorer.FileExplorerControls.TreeView;
            if (visibleControls.Items[4].Selected)
                explorerControls |= Telerik.Web.UI.FileExplorer.FileExplorerControls.ContextMenus;
            return explorerControls;

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            FileExplorer1.Configuration.ViewPaths = FileExplorer1.Configuration.UploadPaths = FileExplorer1.Configuration.DeletePaths = new string[] { "~/Upload" };
            FileExplorer1.InitialPath = Page.ResolveUrl("~/Upload/undelete.me");
        }

        protected void btnTemplate_Click(object sender, EventArgs e)
        {
            FileExplorer1.Configuration.ViewPaths = FileExplorer1.Configuration.UploadPaths = FileExplorer1.Configuration.DeletePaths = new string[] { "~/Template" };
            FileExplorer1.InitialPath = Page.ResolveUrl("~/Template/undelete.me");
        }

        protected void btnTmp_Click(object sender, EventArgs e)
        {
            FileExplorer1.Configuration.ViewPaths = FileExplorer1.Configuration.UploadPaths = FileExplorer1.Configuration.DeletePaths = new string[] { "~/Tmp" };
            FileExplorer1.InitialPath = Page.ResolveUrl("~/Tmp/undelete.me");
        }



    }
}
